﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using Library;
using System.Data;

/*
-- =============================================
-- Author:		Duong Hoang Vinh
-- Create date: 8/11/2011
-- Description:	Role Class
-- =============================================
*/

namespace BusinessLayer
{
   public class Role
    {
    #region "Variables and Properties"
    //Variables
        private int role_ID;        
        private string role_Name;        
        private string role_Detail;        
        private string role_Url;        
        private int role_Status;        

    //Properties
        public int Role_ID
        {
            get { return role_ID; }
            set { role_ID = value; }
        }

        public string Role_Name
        {
            get { return role_Name; }
            set { role_Name = value; }
        }

        public string Role_Detail
        {
            get { return role_Detail; }
            set { role_Detail = value; }
        }

        public string Role_Url
        {
            get { return role_Url; }
            set { role_Url = value; }
        }

        public int Role_Status
        {
            get { return role_Status; }
            set { role_Status = value; }
        }
    #endregion

    #region "Initial Methods"
        public Role() { }

        public Role(SqlDataReader sdr)
        {
            if (sdr != null && !sdr.IsClosed)
            {
                Role_ID = sdr.GetInt32(0);
                if (sdr.IsDBNull(1))
                    Role_Name = sdr.GetString(1);
                if (sdr.IsDBNull(2))
                    Role_Detail = sdr.GetString(2);
                if (sdr.IsDBNull(3))
                    Role_Url = sdr.GetString(3);
                if (sdr.IsDBNull(4))
                    Role_Status = sdr.GetInt32(4);
            }
        }
    #endregion

    #region "Business Methods"
    //Get All Role
        public static List<Role> GetAll()
        {
            DBHelp dbh = new DBHelp();
            string cmd = "tbl_Role_SHOWALL";
            List<Role> lstRole = new List<Role>();
            SqlDataReader sdr = dbh.ExecuteReader(cmd, CommandType.StoredProcedure);

            while (sdr.Read())
            {
                lstRole.Add(new Role(sdr));
            }

            return lstRole;
        }

    //Get One Role
        public static Role GetOne(int idRole)
        {
            DBHelp dbh = new DBHelp();
            string cmd = "tbl_Role_SHOWONE";
            Role role = new Role();
            SqlDataReader sdr;

            dbh.AddParameter("@Role_ID", idRole);
            sdr = dbh.ExecuteReader(cmd, CommandType.StoredProcedure);

            if (sdr.Read())
            {
                role.Role_ID = sdr.GetInt32(0);
                if (sdr.IsDBNull(1))
                    role.Role_Name = sdr.GetString(1);
                if (sdr.IsDBNull(2))
                    role.Role_Detail = sdr.GetString(2);
                if (sdr.IsDBNull(3))
                    role.Role_Url = sdr.GetString(3);
                if (sdr.IsDBNull(4))
                    role.Role_Status = sdr.GetInt32(4);
            }

            return role;
        }

    //Insert Role
        public static void Insert(Role role)
        {
            DBHelp dbh = new DBHelp();
            string cmd = "tbl_Role_INSERT";

            dbh.AddParameter("@Role_Name", role.Role_Name);
            dbh.AddParameter("@Role_Detail", role.Role_Detail);
            dbh.AddParameter("@Role_Url", role.Role_Url);

            dbh.ExecuteNonQuery(cmd, CommandType.StoredProcedure);
        }

    //Update Role
        public static void Update(Role role)
        {
            DBHelp dbh = new DBHelp();
            string cmd = "tbl_Role_UPDATE";

            dbh.AddParameter("@Role_ID", role.Role_ID);
            dbh.AddParameter("@Role_Name", role.Role_Name);
            dbh.AddParameter("@Role_Detail", role.Role_Detail);
            dbh.AddParameter("@Role_Url", role.Role_Url);

            dbh.ExecuteNonQuery(cmd, CommandType.StoredProcedure);
        }

    //Delete Role
        public static void Delete(int idRole)
        {
            DBHelp dbh = new DBHelp();
            string cmd = "tbl_Role_DELETE";

            dbh.AddParameter("@Role_ID", idRole);
            dbh.ExecuteNonQuery(cmd, CommandType.StoredProcedure);
        }
    #endregion
    }
}
